package com.readpost.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class MailUtil {

    @Autowired
    private JavaMailSender mailSender;


    /**
     * 发送邮件
     *
     * @param sendName    发送人
     * @param receiveName 收件人
     * @param title       标题
     * @param content     内容
     * @return true: 发送成功, false: 发送失败
     */
    public  boolean sendMail(String sendName, String receiveName, String title, String content) {
        try{
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(sendName);
            mailMessage.setTo(receiveName);
            mailMessage.setSubject(title);
            mailMessage.setText(content);
            mailSender.send(mailMessage);
            log.info("邮件发送成功，收件人: {}", String.join(", ", receiveName));
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

//    public String authCode(String email){
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userid = (Integer) map.get("userid");
//
//        return
//    }
}
